import { obj_int, is_empty, uReal, for_obj, obj_str, uDb, cline, d_sort } from 'sui';

const load_task = (hDone, setTask) => {
    uDb(hDone, "task/run",
        {
            line: cline(),
            igs: ["G", "R", "auto"],
            count: true,
        },
        r => {
            const ret = [];

            r.forEach(d => {
                if (d.pairst === "close") {
                    return;
                }

                const n = obj_int(d, "psn_num");
                if (n <= 0) {
                    return;
                }

                d["dnum"] = n;

                ret.push(d);
            });

            d_sort(ret, "pstart", true);

            setTask(ret);
        },
        e => setTask(null));
};

const load_box = (hDone, cur, setOut, boxPos) => {
    const task_sn = obj_str(cur, "sn");

    uDb(hDone, "psn/list",
        {
            line: cline(),
            k: task_sn,
        },
        r => {
            const box_out = {};
            const box_in = new Set();

            r.forEach(d => {
                const boxout = obj_str(d, "boxout");
                const boxin = obj_str(d, "boxin");

                if (!boxout || !boxin) {
                    return;
                }

                if (box_out[boxout]) {
                    box_out[boxout].push(d);
                } else {
                    box_out[boxout] = [d];
                }

                box_in.add(boxin);
            });

            const ret_out = [];

            for_obj(box_out, (k, v) => {
                ret_out.push({sn: k, box: v});
            });

            const f = [];
            for (const d of box_in) {
                f.push(d);
            }

            if (!is_empty(f)) {
                uReal(null, `pair/load`,
                    {
                        line: cline(),
                        k: "box",
                        v: f,
                    },
                    r => boxPos(r),
                    e => boxPos(null));
            }

            setOut(ret_out);
        },
        e => setOut(null));
};

const get_rgb = (v) => {
    const tmp = v.split("-");
    const rgb = tmp[1];
    if (!rgb) {
        return [100, 100, 100, "black"];
    }

    const h = rgb[0];
    const n = parseInt(rgb.slice(1), 10);

    const tc = n < 200 ? "white" : "black";

    if (h === "R") {
        return [n, 0, 0, tc];
    } else if (h === "G") {
        return [0, n, 0, tc];
    } else if (h === "B") {
        return [0, 0, n, tc];
    }

    return [100, 100, 100, tc];
};

const split_psn = (v) => {
    if (!v) {
        return v;
    }

    if (v.length !== 8) {
        return v;
    }

    return `${v[0]}${v[1]}${v[2]}${v[4]}${v[6]} - ${v[3]}${v[5]}${v[7]}`;
};

const load_pos = (box_sn, setMain) => {
    if (!box_sn) {
        setMain(null);
        return;
    }

    uDb(null, `file/read/box_${box_sn}`, { line: cline() },
        r => setMain(r),
        e => setMain(null));
};

export {load_pos, split_psn, get_rgb, load_task, load_box};
